package com.lvyerose.viewpagedynamic;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lvyerose.viewpagedynamic.adapter.MeViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    @ViewInject(R.id.viewpage)
    private ViewPager viewPager;
    @ViewInject(R.id.imb_left)
    private ImageButton imageLeft;
    @ViewInject(R.id.imb_right)
    private ImageButton imageRight;
    private int count = 8;
    private int row = 3;
    private int nowPosition = 0;
    private List<View> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewUtils.inject(this);
        LinearLayout layout = null;
        for(int i=0;i<count;i++){
            if(i%row == 0){
                layout = new LinearLayout(this);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT));
                layout.setGravity(Gravity.CENTER);
            }
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.mipmap.yuan);
            layout.addView(imageView);
            if((i+1)%row == 0 || i+1 == count){
                list.add(layout);
            }

        }
        isShow(0);
        viewPager.setAdapter(new MeViewPagerAdapter(list));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                isShow(position);
                nowPosition = position;


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @OnClick({R.id.imb_right , R.id.imb_left})
    public void ImageButListent(View v){
        switch (v.getId()){
            case R.id.imb_left:
                viewPager.setCurrentItem(--nowPosition);
                break;
            case R.id.imb_right:
                viewPager.setCurrentItem(++nowPosition);
                break;
        }
    }

    /**
     *  控制左右箭头是否显示
     * @param position 当前ViewPager的位置
     */
    private void isShow(int position){
        if(position == list.size()-1){
            imageRight.setVisibility(View.GONE);
        }else{
            imageRight.setVisibility(View.VISIBLE);
        }

        if(position == 0){
            imageLeft.setVisibility(View.GONE);
        }else {
            imageLeft.setVisibility(View.VISIBLE);
        }

    }


}
